<?php
class OrderAction extends AdminPublicAction{
	/*======================================================================
	 信息列表
	 ======================================================================================*/
	public function index(){
	
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "order_id";
			$_GET['updown'] = $upDown = "desc";
				
		}
	
		$keyword = I('keyword','');
		
		$status = I('status',-1);
	
		//關鍵字查詢
		if(tep_not_null($keyword)){
			$where['products_name']  = array('like','%'.$keyword.'%');
			$where['tel']  = array('like','%'.$keyword.'%');
			$where['order_num']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		//省份城市筛选
		//$map = get_map_city_id($map);
		$cid = I('provcode','');
		$qid = I('citycode','');
		$aid = I('area_id','');
		if($cid > 0){
			$map['pprovcode'] = $cid;
		}
		if($qid > 0){
			$map['pcitycode'] = $qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$map['parea_id'] = $aid;
		}
		$this->citycode = $map['pcitycode'];
		$this->provcode = $map['pprovcode'];
		$this->area_id = $map['parea_id'];	
	
		//篩選狀態
		if($status >= 0 ) $map['order_status_id'] = $status;
	
		//$map['language_id'] = (int)L('lan_id');
	
		import("ORG.Util.Page");
		$page = 10;
		$count = D('Order')->getOrderCount($map);//->where($map)->count();
		$Page = new Page($count, $page);
		$infoList = D('Order')->getOrderList($map,$sort.' '.$upDown,$Page->firstRow. ',' . $Page->listRows,'');//getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//var_dump($infoList);die;
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
	
		$this->allstatus = M('order_status')->select();
		//$this->category = $category;
		$this->status = $status;
		$this->keyword = $keyword;
		//$this->cate = $cate;
		$this->page = $Page->show();
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_order = 'current';
		$this->display();
	}
	
	//###########################################//
	//确认订单       ，即查看在线邮件，若买家收到邮件长时间未回复，则让订单完成，将金额打给卖家，并更改订单状态                                                                                                          //
	//###########################################//
	public function confirm(){
		$editId = I('editId');
		if(!$editId){
			$this->redirect('Order/index');
		}
		$orderDetail = M('order')->where(array('order_id'=>$editId))->find();
		$moneylog = M('member_moneylog')->where(array('uid'=>$orderDetail['publish_uid'],'order_id'=>$editId))->order('member_moneylog_id desc')->find();
		$data['affect_money'] = $moneylog['affect_money'];
		$data['active_money'] = $moneylog['active_money']+$moneylog['affect_money'];
		$data['freeze_money'] = $moneylog['freeze_money'] - $moneylog['affect_money'];
		$data['uid'] = $moneylog['uid'];
		$data['money_type'] = 8;
		$data['moneylog_affect_date'] = date('Y-m-d H:i:s', time());
		$data['order_id'] = $editId;
		$data['u_ip'] = getip();
		$data['comment'] = '系统查看邮件签收，将金额解冻给卖家';
		$result = M('member_moneylog')->add($data);
		if($result){
			M('member_moneylog')->where(array('member_moneylog_id'=>$moneylog['member_moneylog_id']))->setField('freeze_flag', 1);
			$data1['comment'] = '系统查看邮件签收，将订单状态修改完成';
			$data1['order_status_id'] = 7;
			$result = M('order')->where(array('order_id'=>$editId))->save($data1);
			if($result){
				S('success','处理订单完成成功!');
				$this->redirect('index');
			}else{
				S('err','处理订单完成失败!');
				$this->redirect('index');
			}
		}else{
			S('err','处理订单完成失败!');
			$this->redirect('index');
		}
		
		
		
	}
	
	//###########################################
	//取消订单。若买家买了很久，迟迟不付款，卖家申诉，则取消订单，商品重新发布，,或者买家申诉成功，取消订单
	//###########################################
	public function cancel(){
		$editId = I('editId');
		if(!$editId){
			$this->redirect('Order/index');
		}
		$orderstatus = I('orderstatus');
		$orderDetail = M('order')->where(array('order_id'=>$editId))->find();
		if($orderstatus==1){//只下单，未付款过期订单处理
			$data['order_status_id'] = 11;
		}else{//已付款订单，处理			
			$result= D('MemberMoneylog')->MoneychangeS($orderDetail);
			if(!$result){//若打款给买家失败，取消上调记录
					//M('member_moneylog')->where(array('member_moneylog_id'=>$result))->delete();
					S('err','处理订单取消失败!');
					$this->redirect('index');
			}
			$data['order_status_id'] = 12;
		}
		
		$data['comment'] = '系统取消订单'; 
		$result = M('order')->where(array('order_id'=>$editId))->save($data);
		if($result){
			$result = M('products')->where(array('products_id'=>$orderDetail['products_id']))->setField('show_flag', 0);
			if($result){
				S('success','处理订单取消成功!');
				$this->redirect('index');
			}else{
				S('err','处理订单取消成功,商品重新发布失败!');
				$this->redirect('index');
			}
		}else{
			S('err','处理订单取消失败!');
			$this->redirect('index');
		}
	}
	
	/**
	 * 订单导出
	 */
	public function ToExcel(){
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "order_id";
			$_GET['updown'] = $upDown = "desc";
				
		}
	
		$keyword = I('keyword','');
		
		$status = I('status',-1);
	
		//關鍵字查詢
		if(tep_not_null($keyword)){
			$where['products_name']  = array('like','%'.$keyword.'%');
			$where['tel']  = array('like','%'.$keyword.'%');
			$where['order_num']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		//省份城市筛选
		//$map = get_map_city_id($map);
		$cid = I('provcode','');
		$qid = I('citycode','');
		$aid = I('area_id','');
		if($cid > 0){
			$map['pprovcode'] = $cid;
		}
		if($qid > 0){
			$map['pcitycode'] = $qid;
		}
		// 社区筛选
		if ($aid > 0) {
			$map['parea_id'] = $aid;
		}
		$this->citycode = $map['pcitycode'];
		$this->provcode = $map['pprovcode'];
		$this->area_id = $map['parea_id'];	
	
		//篩選狀態
		if($status >= 0 ) $map['order_status_id'] = $status;
	
		$result = D('Order')->getOrderList($map,$sort.' '.$upDown,'','');//getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//var_dump($result);die;
		$xls_name = "商品交易列表";
		header("Content-type:application/vnd.ms-excel;charset=UTF-8");
		header("Content-Disposition:attachment;filename=$xls_name.xls");
	
		$table = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"	xmlns="http://www.w3.org/TR/REC-html40">
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
            <style id="Classeur1_16681_Styles"></style>
            </head>';
		$table .= '<table style="border:1px solid #000; border-spacing:0px; width:1000px;text-align:left;border-collapse: collapse;">';
		$table .= '<thead><tr style="background:#c6c6c6;height:40px;"><th style="border:1px solid #000;">订单编号</th><th style="border:1px solid #000;">日期</th><th style="border:1px solid #000;">商品名称</th><th style="border:1px solid #000;">金额</th><th style="border:1px solid #000;">卖家</th><th style="border:1px solid #000;">卖家电话</th><th style="border:1px solid #000;">买家</th><th style="border:1px solid #000;">买家电话</th><th style="border:1px solid #000;">交易方式</th><th style="border:1px solid #000;">支付状态</th><th style="border:1px solid #000;">物流单号</th><th style="border:1px solid #000;">快递公司</th><th style="border:1px solid #000;">交易状态</th></thead>';
		$table .= '<tbody>';
		foreach($result as $key=>$value){
			if(($key+1)%2==0){
				$table .= "<tr style='background:#a9c6c9'>";
			}else{
				$table .= "<tr>";
			}
	
			$table .= "<td style='border:1px solid #000;'>".$value['order_num']."</td>";
			$table .= "<td style='border:1px solid #000;'>".$value['order_date_added'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['products_name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['pay_price'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['sdetail']['name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['sdetail']['tel'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['bdetail']['name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['bdetail']['tel'].'</td>';
			if($value['order_type']==1){
				$table .= "<td style='border:1px solid #000;'>线下交易</td>";
			}elseif($value['order_type']==0){
				$table .= "<td style='border:1px solid #000;'>线上交易</td>";
			}
			if($value['order_type']==0&&$value['order_status_id']>1){
				$table .= "<td style='border:1px solid #000;'>已支付</td>";
			}elseif(($value['order_type']==0 && $value['order_status_id']==1)||$value['order_type']==1){				
				$table .= "<td style='border:1px solid #000;'></td>";
			}
			$table .= "<td style='border:1px solid #000;'>".$value['delivery_num'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['delivery_company'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['os_name'].'</td>';			
			$table .= '</tr>';
		}
		$table .= '</tbody>';
		$table .= '</table>';
		echo $table;
	}
}